$(document).ready(function() {
    var oldscroll = 0, newscroll = 0;
    var hdschHeight = $("#hidesearch").outerHeight();
    $(".holder-input").placeholder();  
    $(".foodlayer").hover(  //鼠标经过弹出商品介绍文字
            function() {
                var sdheight = $(this).find(".areatips_txt").height();
                $(this).find(".areatips").height(sdheight + 20);
                $(this).addClass("on");
            },
            function() {
                $(this).removeClass("on");
            }
    );

    $(".skuselect > li > a").click(function() {  // 商品sku选择，价格变动，样式变动
        if ($(this).parent("li").hasClass("selected")) {
            $(this).parent("li").removeClass("selected");
        } else {
            $(this).parents("li").siblings("li").removeClass("selected");
            $(this).parent("li").addClass("selected");
        }
        var skuobj = $(".tb-sku").find("li.selected a");
//        alert(skuobj.length);
        if (skucount == skuobj.length) {
            $.each(skuobj, function(i) {
                $("#skuprice").html(SKUResult[skuobj.eq(i).attr("skuvalueid")]['skuPrice']);
            });
        } else {
            $("#skuprice").html(oldprice);
        }
//        SKUResult['1,4']['skuPrice']
    });

//    $(".tab-a > a").click(function() {
//        var tabaid = $(this).index();
//        $(this).siblings("a").removeClass('on');
//        $(this).addClass('on');
//        var pboxs = $(this).parents(".tab-out").find(".tab-box");
//        pboxs.hide();
//        pboxs.eq(tabaid).show();
//    });

    $(".hotcontent").mouseover(function() {  //热门商品显示效果
        $(this).siblings(".hotcontent").find(".hotbody").hide();
        $(this).find(".hotbody").show();
    });

    $(window).scroll(function() {  //迷你搜索栏随滚动条弹出/收起
        newscroll = $(this).scrollTop();
        if (oldscroll < newscroll) {
            if (newscroll >= 250) {
                $("#hidesearch").stop().animate({
                    top: '0px',
                }, 100);
            }
        } else {
            if (newscroll < 250) {
                $("#hidesearch").stop().animate({
                    top: '-' + hdschHeight + 'px',
                }, 100);
            }
        }
        setTimeout(function() {
            oldscroll = newscroll;
        }, 1000)
    });

    $(".tags").hover(function() {  //商品详细页面 tag 鼠标经过效果
        $(this).find(".cmicons").addClass("on");
    }, function() {
        $(this).find(".cmicons").removeClass("on");
    });

    $(".less,.incr").click(function(index) {  //商品数量
        var nowcount = $(this).siblings("input:text").val();
        console.log(index);
    });

    $(".add-storelist").click(function() {  // 添加到购物车
        var producthtml = "<li class=\"cl\"><div class=\"z name\">木瓜炖雪蛤</div><div class=\"z count\">×1</div><div class=\"z price\">￥3.00</div><div class=\"z btn\"><a href=\"#\" class=\"cmicons\"></a></div></li>";
        $(".store-list-box").append(producthtml);
        $(".store-list-out").show();
    });
});

function addshadow() {
    var sHeight = $(document).height();
    var sWidth = $(document).width();
    $("body").append("<div id=\"shadowbox\"></div>");
    $("#shadowbox").css({
        "height": sHeight,
        "width": sWidth
    });
}

function ajaxWindow(title, url) {
    var sHeight = $(document).height();
    var sWidth = $(document).width();
    addshadow();
    $("#append_parent").html("<div id=\"layer_out\"></div>");
    var html = '';
    html += '<div class="layer_header c_b">' + title;
    html += '<a href="javascript:;" onclick="hideWindow();" class="layer_hide cmicons"></a>';
    html += '</div>';
    html += '<div id="layer_body"></div>';
    $("#layer_out").append(html);
    $("#layer_out").css({
        "top": 200,
        "left": (sWidth - 500) / 2
    });
    $.ajax({
        url: url,
        cache: false,
        type: 'get',
        dataType: 'html',
        success: function(html) {
            $("#layer_body").html(html);
            $("#append_parent").show();
        }
    });
}

function hideWindow() {
    $("#layer_out").remove("div");
    $("#append_parent").hide();
    $("#shadowbox").remove("div");
}

function ajaxPost(url, form, target, aptype, ocation) {
    $.ajax({
        url: 'ajax/?mod=' + url,
        cache: false,
        type: 'post',
        dataType: 'html',
        processData: false,
        data: $("#" + form).serialize(),
        success: function(html) {
            if (location) {
                location.href = location;
            } else {
                $("#" + target).aptype(html);
            }
        }
    });
}

function getObjKeys(obj) {
    if (obj !== Object(obj))
        throw new TypeError('Invalid object');
    var keys = [];
    for (var key in obj)
        if (Object.prototype.hasOwnProperty.call(obj, key))
            keys[keys.length] = key;
    return keys;
}
function initSKU() {
    var i, j, skuKeys = getObjKeys(data);
    for (i = 0; i < skuKeys.length; i++) {
        var skuKey = skuKeys[i];//一条SKU信息key
        var sku = data[skuKey]; //一条SKU信息value
        var skuKeyAttrs = skuKey.split(","); //SKU信息key属性值数组
        var len = skuKeyAttrs.length;


        //对每个SKU信息key属性值进行拆分组合
        var combArr = arrayCombine(skuKeyAttrs);
        for (j = 0; j < combArr.length; j++) {
            add2SKUResult(combArr[j], sku);
        }

        //结果集接放入SKUResult
        SKUResult[skuKey] = {
            skuPrice: [sku.skuPrice]
        }
    }
}
function arrayCombine(targetArr) {
    if (!targetArr || !targetArr.length) {
        return [];
    }

    var len = targetArr.length;
    var resultArrs = [];

    // 所有组合
    for (var n = 1; n < len; n++) {
        var flagArrs = getFlagArrs(len, n);
        while (flagArrs.length) {
            var flagArr = flagArrs.shift();
            var combArr = [];
            for (var i = 0; i < len; i++) {
                flagArr[i] && combArr.push(targetArr[i]);
            }
            resultArrs.push(combArr);
        }
    }

    return resultArrs;
}
function getFlagArrs(m, n) {
    if (!n || n < 1) {
        return [];
    }

    var resultArrs = [],
            flagArr = [],
            isEnd = false,
            i, j, leftCnt;

    for (i = 0; i < m; i++) {
        flagArr[i] = i < n ? 1 : 0;
    }

    resultArrs.push(flagArr.concat());

    while (!isEnd) {
        leftCnt = 0;
        for (i = 0; i < m - 1; i++) {
            if (flagArr[i] == 1 && flagArr[i + 1] == 0) {
                for (j = 0; j < i; j++) {
                    flagArr[j] = j < leftCnt ? 1 : 0;
                }
                flagArr[i] = 0;
                flagArr[i + 1] = 1;
                var aTmp = flagArr.concat();
                resultArrs.push(aTmp);
                if (aTmp.slice(-n).join("").indexOf('0') == -1) {
                    isEnd = true;
                }
                break;
            }
            flagArr[i] == 1 && leftCnt++;
        }
    }
    return resultArrs;
}
function add2SKUResult(combArrItem, sku) {
    var key = combArrItem.join(",");
    if (SKUResult[key]) {//SKU信息key属性·
        SKUResult[key].count += sku.count;
        SKUResult[key].prices.push(sku.skuPrice);
    } else {
        SKUResult[key] = {
            skuPrice: [sku.skuPrice]
        };
    }
}